/**
 * @author        Nicolas Bottarini <nicolasbottarini@gmail.com>, 404 <http://www.proyecto404.com>
 * @version       $Rev: 3 $
 * @lastrevision  $Date: 2011-09-12 16:45:37 +0000 (Mon, 12 Sep 2011) $	 
 */
package com.proyecto404.core.util {

	/**
	 * @static
	 */
	public class StringHelper {

		/**
		 * Trim/Strip unwanted spaces from the string.
		 *
		 * @param String  String to be trimmed
		 * @param Boolean  default is true, if false is used will do only LTrim and RTrim.
		 * @return    Trimmed String
		 */
		public static function trim(inputStr:String, extraWhiteSpace:Boolean = true):String {
			var temp:String = inputStr;
			var obj:RegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
			if (obj.test(temp)) {
				temp = temp.replace(obj, '$2');
			}
			
			if(extraWhiteSpace) {
				var obj1 : RegExp = / +/g;
				temp = temp.replace(obj1, " ");
				if (temp == " ") {
					temp = "";
				}
			}
			
			return temp;
		}
		
		public static function formatWith(stringFormat:String, ...rest):String {
			for(var i:int = 0 ; i < rest.length; i++) {
				stringFormat = stringFormat.replace('{' + (i) + '}', rest[i]);
			}
			return stringFormat;
		}
		
		public static function startsWith(string:String, start:String):Boolean {
			var tmpStart:String = string.substring(0, start.length);
			return tmpStart == start;
		}
		
		public static function endsWith(string:String, ending:String):Boolean {
			var tmpEnding:String = string.substring(string.length - ending.length);
			return tmpEnding == ending;
		}
		
		public static function stripTags(string:String):String {
			var removeHTML:RegExp = new RegExp("<[^>]*>", "gi");
			return string.replace(removeHTML, "");
		}
		
		public static function isNullOrEmpty(string:String):Boolean {
			return string == null || string == "";
		}
		
		public static function firstLowercase(string:String):String {
			if (isNullOrEmpty(string)) {
				return "";
			}
			return string.charAt(0).toLowerCase() + string.substr(1);
		}
		
		public static function fillWithZeroes(string:String, length:int):String {
			var diff:int = length - string.length;
			var newStr:String = "";
			for(var i:int = 0;i < diff;i++) { newStr += '0'; }
			return newStr + string;
		}
		
		public static function isValidEmail(email:String):Boolean {
			var emailExpression:RegExp=/^[a-z0-9][-._a-z0-9]*@([a-z0-9][-_a-z0-9]*\.)+[a-z]{2,6}$/;
			return emailExpression.test(email);
		}
	}
}